package com.sharp.authorization.controller.login;

import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.annotation.captcha.CaptchaVerify;
import com.sharp.authorization.annotation.msg.MsgCodeVerify;
import com.sharp.authorization.service.login.LoginService;
import com.sharp.authorization.vo.login.EmailRegReq;
import com.sharp.authorization.vo.login.LoginReq;
import com.sharp.authorization.vo.login.LoginRes;
import com.sharp.authorization.vo.login.MsgLoginReq;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.vo.common.CommonResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Title: RegisterController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/4/18 11:58
 * @description Project Name: Mall
 * @Package: com.srct.service.account.controller.login
 */
@Auth
@Api(value = "账号注册", tags = {"用户账户操作-账号注册"})
@RestController("RegisterController")
@RequestMapping(value = "/register")
public class RegisterController {

    @Resource
    private LoginService loginService;

    @ApiOperation(value = "账号密码注册", notes = "利用账号密码注册")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "用户名", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "password", value = "密码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "graphCode", value = "图形验证码", required = true)})
    @CaptchaVerify("LOGIN_CAPTCHA")
    public ResponseEntity<Response<LoginRes>> register(LoginReq loginReq) {
        LoginRes res = loginService.register(loginReq.getLoginId(), loginReq.getPassword());
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "短信验证码注册", notes = "利用短信验证码注册")
    @RequestMapping(value = "/msg", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "phoneNumber", value = "手机号码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "loginMsgCode", value = "短信验证码", required = true)})
    @MsgCodeVerify("LOGIN_MSG_CODE")
    public ResponseEntity<Response<LoginRes>> login(MsgLoginReq msgLoginReq) {
        LoginRes res = loginService.loginOrReg(msgLoginReq.getPhoneNumber());
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "邮箱注册", notes = "利用邮箱注册")
    @RequestMapping(value = "/email", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "id", value = "用户名", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "email", value = "邮箱", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "graphCode", value = "图形验证码", required = true)})
    @CaptchaVerify("LOGIN_CAPTCHA")
    public ResponseEntity<Response<CommonResVO>> emailRegister(EmailRegReq loginReq) {
        loginService.emailRegister(loginReq.getId(), loginReq.getEmail());
        return ResponseHandler.commonResponse();
    }

}
